package net.dgg.dggcloud.service.jrcrm.aspect;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import net.dgg.dggcloud.core.util.DateUtil;
import net.dgg.dggcloud.service.common.base.SessionManager;
import net.dgg.dggcloud.service.entity.Role;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.iboss.constant.SysRecordConstant;
import net.dgg.dggcloud.service.jrcrm.constant.OpreateEnum;
import net.dgg.dggcloud.service.jrcrm.constant.OpreateTypeEnum;
import net.dgg.dggcloud.service.jrcrm.service.log.SysModifyAuthRecordService;
import net.dgg.dggcloud.service.jrcrm.utils.IpUtil;
import net.dgg.dggcloud.service.service.RoleService;
import net.dgg.framework.tac.mq.rabbitmq.queue.DggMessageSendUtil;
import net.dgg.framework.tac.utils.idutil.DggKeyWorker;

/**
 * 角色删除aop
 * 
 * @Title: RoleDelAspect
 * @Description:
 * @Auther: 雷力
 * @Date: 2018-11-23 09:49:08
 *
 */
@Aspect
@Component
public class RoleDelAspect {
	private Logger logger = Logger.getLogger(RoleDelAspect.class);

	@Autowired
	private RoleService roleService;

	@Pointcut("execution(public * net.dgg.dggcloud.platform.action.RoleController.del(..))")
	public void webLog() {
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Before("webLog()")
	public void before(JoinPoint jp) {
		try {
			ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			HttpServletRequest request = attributes.getRequest();
			User user = SessionManager.getCurrentUser();
			Object[] objs = jp.getArgs();
			Long roleId = Long.valueOf(objs[0].toString());
			Role role = roleService.find4Id(roleId);

			Map record = new HashMap<>();
			record.put("id", DggKeyWorker.nextId());
			record.put("ip", IpUtil.getIPAddress(request));
			record.put("opreate", OpreateEnum.ROLEMANAGER.getOpreate());
			record.put("opreateTime", DateUtil.dateToString(new Date()));
			record.put("userLoginname", user.getLoginName());
			record.put("userName", user.getName());
			record.put("vuserLoginname", role.getRoleKey());
			record.put("vuserName", role.getName());
			record.put("opreateType", OpreateTypeEnum.DELETE.getType());
			record.put("modifyContent", "已移除");
			record.put("clazz", SysModifyAuthRecordService.class);
			logger.info("======================>发送消息...");
			DggMessageSendUtil.putMessage(SysRecordConstant.QUEUENAME, record);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
